package com.property.manage.controller.system;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.property.manage.model.system.UserDetail;
import com.property.manage.model.system.UserLoginInfo;
import com.property.manage.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Validated
public class UserController {

    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public UserLoginInfo login(@ApiParam("账号") @RequestParam("userName") @NotBlank(message = "请输入账号") String userName,
                               @ApiParam("密码") @RequestParam("pwd") @NotBlank(message = "请输入密码") String pwd){

        return userService.login(userName, pwd);
    }

    @ApiOperation("用户列表")
    @GetMapping("/page")
    public CustomPage<UserDetail> userPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                           @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                           @ApiParam("账号") @RequestParam(value = "userName", required = false) String userName){
        return userService.userPage(pageNo, pageSize, userName);
    }

    @ApiOperation("添加账号")
    @PostMapping("/add")
    public void add(@ApiParam("账号") @RequestParam("userName") @NotBlank(message = "请输入账号") String userName,
                    @ApiParam("密码") @RequestParam("pwd") @NotBlank(message = "请输入密码") String pwd,
                    @ApiParam("角色编号") @RequestParam(value = "roleId", required = false)  Integer roleId,
                    @ApiParam("联系方式") @RequestParam(value = "mobile", required = false) String mobile){
        userService.add(userName, pwd, roleId, mobile);
    }

    @ApiOperation("修改账号")
    @PostMapping("/update")
    public void update(@ApiParam("账户id") @RequestParam("accountId") @NotNull(message = "账户id为空") Integer accountId,
                       @ApiParam("账号") @RequestParam(value = "userName", required = false) String userName,
                       @ApiParam("密码") @RequestParam(value = "pwd", required = false) String pwd,
                       @ApiParam("角色编号") @RequestParam(value = "roleId", required = false)  Integer roleId,
                       @ApiParam("联系方式") @RequestParam(value = "mobile", required = false) String mobile){
        userService.update(accountId, userName, pwd, roleId, mobile);
    }

    @ApiOperation("删除账号")
    @PostMapping("/delete")
    public void delete(@ApiParam("账户id") @RequestParam("accountId") @NotNull(message = "账户id为空") Integer accountId){
        userService.delete(accountId);
    }

}
